package day19;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

//jdk7新特性-try-with-resources：自动关闭流
public class Test_JDK7 {
    public static void main(String[] args) throws IOException {
//        method();//IO 练习
        method2();//IO 练习
    }
    //jdk7后 try-with-resources：自动关闭流
    private static void method2() {
        try(
            InputStream in = System.in;
            BufferedInputStream bis = new BufferedInputStream(in);
            ) {
            //1,创建读取流对象
            //2,开始读取
            int b = 0;
            //有数据就读,没数据就返回-1
            while( ( b = bis.read() ) != -1){
                System.out.println((char) b );
            }
   //jdk7提供的可以一次性catch多个 类型的异常 |分隔
        }catch(IOException|ArithmeticException|ArrayIndexOutOfBoundsException e){
            e.printStackTrace();
        }
    }
    //jdk7前
    //--需求:接收键盘输入的数据,并打印
    public static void method()  {
        InputStream in = null ;
        BufferedInputStream bis = null;
        try {
            //1,创建读取流对象
            in = System.in;
            bis = new BufferedInputStream(in);
            //2,开始读取
            int b = 0;
            //有数据就读,没数据就返回-1
            while( ( b = bis.read() ) != -1){
                System.out.println((char) b );
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {//一定要执行的代码放finally
            //3,释放资源
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
